package com.cicigodd.brick.plug.system.menu.controller;

import com.cicigodd.brick.plug.annotation.repeatsubmit.RepeatSubmit;
import com.cicigodd.brick.plug.annotation.resource.DeleteResource;
import com.cicigodd.brick.plug.annotation.resource.PutResource;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.cicigodd.brick.plug.common.response.Result;
import com.cicigodd.brick.plug.common.response.ResultGenerator;
import com.cicigodd.brick.plug.annotation.resource.PostResource;
import com.cicigodd.brick.plug.annotation.resource.GetResource;
import com.cicigodd.brick.plug.common.tree.node.ElementTreeNode;
import com.cicigodd.brick.plug.system.menu.request.SysMenuAddRequest;
import com.cicigodd.brick.plug.system.menu.request.SysMenuDeleteRequest;
import com.cicigodd.brick.plug.system.menu.request.SysMenuDetailRequest;
import com.cicigodd.brick.plug.system.menu.request.SysMenuEditRequest;
import com.cicigodd.brick.plug.system.menu.request.SysMenuListRequest;
import com.cicigodd.brick.plug.system.menu.response.RouterResponse;
import com.cicigodd.brick.plug.system.menu.response.SysMenuDetailResponse;
import com.cicigodd.brick.plug.system.menu.response.SysMenuListResponse;
import com.cicigodd.brick.plug.system.menu.service.SysMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import java.util.List;


@Api(tags = "菜单管理")
@RestController
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @RepeatSubmit
    @ApiOperation(value = "菜单管理添加", notes = "菜单管理添加")
    @PostResource(name = "菜单管理添加", path = "/sysMenu/add")
    public Result add(@RequestBody @Validated SysMenuAddRequest sysMenuAddRequest) {
        sysMenuService.add(sysMenuAddRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "菜单管理删除", notes = "根据id删除")
    @DeleteResource(name = "菜单管理删除", path = "/sysMenu/delete")
    public Result delete(@Validated SysMenuDeleteRequest sysMenuDeleteRequest) {
        sysMenuService.delete(sysMenuDeleteRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "菜单管理编辑", notes = "菜单管理编辑")
    @PutResource(name = "菜单管理编辑", path = "/sysMenu/edit")
    public Result edit(@RequestBody @Validated SysMenuEditRequest sysMenuEditRequest) {
        sysMenuService.edit(sysMenuEditRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "菜单管理详情", notes = "根据id查询")
    @GetResource(name = "菜单管理详情", path = "/sysMenu/detail")
    public Result<SysMenuDetailResponse> detail(SysMenuDetailRequest sysMenuDetailRequest) {
        SysMenuDetailResponse detail = sysMenuService.detail(sysMenuDetailRequest);
        return ResultGenerator.successResult(detail);
    }


    @ApiOperation(value = "菜单管理分页列表", notes = "菜单管理分页列表")
    @GetResource(name = "菜单管理分页列表", path = "/sysMenu/list")
    public Result<PageResult<SysMenuListResponse>> list(SysMenuListRequest sysMenuListRequest) {
        PageResult<SysMenuListResponse> pageList =  sysMenuService.list(sysMenuListRequest);
        return ResultGenerator.successResult(pageList);
    }

    @ApiOperation(value = "获取菜单路由", notes = "获取菜单路由")
    @GetResource(name = "获取菜单路由", path = "/sysMenu/getRouters")
    public Result<List<RouterResponse>> getRouters() {
        List<RouterResponse> list =  sysMenuService.getRouters();
        return ResultGenerator.successResult(list);
    }


    @ApiOperation(value = "获取系统菜单树", notes = "获取系统菜单树，用于新增，编辑时选择上级节点")
    @GetResource(name = "获取系统菜单树", path = "/sysMenu/tree")
    public Result<List<ElementTreeNode>> tree() {
        List<ElementTreeNode> list = sysMenuService.tree();
        return ResultGenerator.successResult(list);
    }

    @ApiOperation(value = "获取系统全部菜单树", notes = "获取系统菜单树，用于权限绑定")
    @GetResource(name = "获取系统全部菜单树", path = "/sysMenu/treeAll")
    public Result<List<ElementTreeNode>> treeAll() {
        List<ElementTreeNode> list = sysMenuService.treeAll();
        return ResultGenerator.successResult(list);
    }
}
